package com.nowadays.mobile.utils;

import android.content.Context;

import com.nowadays.mobile.model.Focus;
import com.nowadays.mobile.model.FocusDayList;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FocusUtils {
  private static final String TIME_MASK = "yyyy.MM.dd HH:mm";
  public static final String COLON = ":";
  public static final String SPACE = " ";
  public static final String DOUBLE_ZERO = "00";
  public static final String PRE_ZERO = "0";

  public static final int SECONDS_IN_MIN = 60;
  public static final int MINUTES_IN_HOUR = 60;
  public static final int MILLIS_IN_SECOND = 1000;

  public static String formateDate(long seconds) {
    Date date = new Date(seconds * MILLIS_IN_SECOND);
    SimpleDateFormat formatter = new SimpleDateFormat(TIME_MASK);
    return formatter.format(date);
  }

  /**
   * 整理专注列表，每天的专注均有2项，一项为每月的专注总览，一项为包含专注详情的专注明细
   *
   * @param focusLists 未按天整理过的专注列表
   * @return focusDayList 每月总结及每天详情的列表
   */
  public static ArrayList<FocusDayList> listFocusByDay(List<Focus> focusLists) {
    if (focusLists == null || focusLists.size() == 0) {
      return null;
    }
    ArrayList<FocusDayList> focusDayLists = new ArrayList<>();
    for (Focus focus : focusLists) {
      long endTime = focus.getEndTime();
      String dateFormatted = formateDate(endTime);
      String strDay = dateFormatted.substring(TIME_MASK.indexOf("d"), TIME_MASK.lastIndexOf("d") + 1);
      String strYearMonth = dateFormatted.substring(TIME_MASK.indexOf("y"), TIME_MASK.lastIndexOf("M") + 1);

      //查找是否存在某月的专注总览
      FocusDayList focusDaySum = getFocusDayByTime(focusDayLists, strDay, strYearMonth, ConstsDefine.FOCUS_TYPE.TYPE_SUM);

      if (focusDaySum != null) {
        //update sum
        String totalDuation = focusDaySum.getTotalDuration();
        int duration = Integer.parseInt(totalDuation) + focus.getDuration();
        focusDaySum.setTotalDuration(String.valueOf(duration));

        //查找是否存在某天的专注明细
        FocusDayList focusDayDetail = getFocusDayByTime(focusDayLists, strDay, strYearMonth, ConstsDefine.FOCUS_TYPE.TYPE_DETAIL);
        //update detail
        if (focusDayDetail != null) {
          focusDayDetail.setTotalDuration(String.valueOf(duration));
          List<Focus> focusList = focusDayDetail.getFocusList();
          focusList.add(focus);
          continue;
        }
      } else {
        //add sum
        focusDaySum = new FocusDayList();
        focusDaySum.setYearMonth(strYearMonth);
        focusDaySum.setType(ConstsDefine.FOCUS_TYPE.TYPE_SUM);
        focusDaySum.setTotalDuration(String.valueOf(focus.getDuration()));
        focusDayLists.add(focusDaySum);
      }
      //add detail
      FocusDayList focusDayDetail = (FocusDayList) focusDaySum.clone();
      focusDayDetail.setType(ConstsDefine.FOCUS_TYPE.TYPE_DETAIL);
      focusDayDetail.setDay(strDay);
      List<Focus> focusList = new ArrayList<>();
      focusList.add(focus);
      focusDayDetail.setFocusList(focusList);
      focusDayLists.add(focusDayDetail);
    }
    return focusDayLists;
  }

  private static FocusDayList getFocusDayByTime(ArrayList<FocusDayList> focusDayLists, String strDay, String strYearMonth, int paramType) {
    if (focusDayLists == null || focusDayLists.size() == 0) {
      return null;
    }
    for (FocusDayList focusDayList : focusDayLists) {
      String yearMonth = focusDayList.getYearMonth();
      String day = focusDayList.getDay();
      int type = focusDayList.getType();
      if (yearMonth.equals(strYearMonth) && type == paramType && (type == ConstsDefine.FOCUS_TYPE.TYPE_SUM || day.equals(strDay))) {
        return focusDayList;
      }
    }
    return null;
  }

  public static String displayInHours(int totalSeconds) {
    return displayInHours(totalSeconds, true);
  }

  /**
   * 格式化秒数为小时为最大单位的时间
   * @param totalSeconds 总的秒数
   * @param showColon 小时、分钟、秒数之间是否显示冒号，否则显示空格
   * @return  格式化的时间如23:10:11或 23 10 11
   */
  public static String displayInHours(int totalSeconds, boolean showColon) {
    int hours = totalSeconds / (SECONDS_IN_MIN * MINUTES_IN_HOUR);
    String hourStr = formatTimeStr(hours);
    int minutes = (totalSeconds - hours * SECONDS_IN_MIN * MINUTES_IN_HOUR) / MINUTES_IN_HOUR;
    String minuteStr = formatTimeStr(minutes);
    String returnStr;
    if(showColon) {
      returnStr = hourStr + COLON + minuteStr + COLON + DOUBLE_ZERO;
    } else {
      returnStr = hourStr + SPACE + minuteStr + SPACE +DOUBLE_ZERO;
    }
    return returnStr;
  }

  public static String displayInMinutes(int totalSeconds) {
    return displayInMinutes(totalSeconds, true);
  }

  private static String displayInMinutes(int totalSeconds, boolean showColon) {
    int minute = totalSeconds / SECONDS_IN_MIN;
    String minutesStr = formatTimeStr(minute);
    int seconds = totalSeconds - minute * SECONDS_IN_MIN;
    String secondsStr = formatTimeStr(seconds);
    String returnStr;
    if(showColon) {
      returnStr = minutesStr + COLON + secondsStr;
    } else {
      returnStr = minutesStr + SPACE + secondsStr;
    }
    return returnStr;
  }

  public static String formatTimeStr(int time) {
    return time == 0 ? DOUBLE_ZERO : (time < 10 ? PRE_ZERO + time : "" + time);
  }

  public static int getDurSeconds(int durMinutes) {
    return durMinutes * SECONDS_IN_MIN;
  }

  public static int getMinutesOfSeconds(int seconds) {
    return seconds / SECONDS_IN_MIN;
  }

  /**
   * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
   */
  public static float dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (dpValue * scale + 0.5f);
  }
}
